// #include "ring_queue.hpp"
// #include <pthread.h>
// #include <stdlib.h>
// #include <unistd.h>

// using namespace ns_ring_queue;

// void *consumer(void *args)
// {
//     RingQueue<int> *rq = (RingQueue<int> *)args;
//     while (true)
//     {
//         int data = 0;
//         rq->pop(&data);
//         std::cout << "消费者消费数据：" << data <<  " 线程id: " << pthread_self() << std::endl;
//         sleep(1);
//     }
// }

// void *productor(void *args)
// {
//     RingQueue<int> *rq = (RingQueue<int> *)args;
//     while (true)
//     {
//         int data = rand() % 20 + 1;//[1,20]
//         std::cout << "生产数据：" << data <<  " 线程id: " << pthread_self() << std::endl;
//         rq->push(data);
//         //sleep(1);
//     }
// }

// int main()
// {
//     srand((long long)time(nullptr));
//     pthread_t c1;
//     pthread_t c2;
//     pthread_t c3;
//     pthread_t c4;
//     pthread_t p1;
//     pthread_t p2;
//     pthread_t p3;
//     pthread_t p4;
//     RingQueue<int> *rq = new RingQueue<int>;

//     pthread_create(&c1, nullptr, consumer, rq);
//     pthread_create(&c2, nullptr, consumer, rq);
//     pthread_create(&c3, nullptr, consumer, rq);
//     pthread_create(&c4, nullptr, consumer, rq);
//     pthread_create(&p1, nullptr, productor, rq);
//     pthread_create(&p2, nullptr, productor, rq);
//     pthread_create(&p3, nullptr, productor, rq);
//     pthread_create(&p4, nullptr, productor, rq);

//     pthread_join(c1, nullptr);
//     pthread_join(c2, nullptr);
//     pthread_join(c3, nullptr);
//     pthread_join(c4, nullptr);
//     pthread_join(p1, nullptr);
//     pthread_join(p2, nullptr);
//     pthread_join(p3, nullptr);
//     pthread_join(p4, nullptr);
//     return 0;
// }


#include "ring_queue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
using namespace ns_task;
using namespace ns_ring_queue;

void *consumer(void *args)
{
    RingQueue<Task> *rq = (RingQueue<Task> *)args;
    while (true)
    {
        Task t;
        rq->pop(&t);
        t();
    }
}

void *productor(void *args)
{
    RingQueue<Task> *rq = (RingQueue<Task> *)args;
    std::string ops = "+-*/%";
    while (true)
    {
        int x = rand()%20 + 1;//[1, 20]
        int y = rand()%10 + 1;//[1, 10]
        char op = ops[rand()%5];
        std::cout << "生产者派发任务" << x << " " << y << " " << op << " " << \
         "当前线程 " << pthread_self() << std::endl;
        Task t(x, y, op);
        rq->push(t);
        sleep(1);
    }
}

int main()
{
    srand((long long)time(nullptr));
    pthread_t c1;
    pthread_t c2;
    pthread_t c3;
    pthread_t c4;
    pthread_t p1;
    pthread_t p2;
    pthread_t p3;
    pthread_t p4;
    RingQueue<Task> *rq = new RingQueue<Task>;

    pthread_create(&c1, nullptr, consumer, rq);
    pthread_create(&c2, nullptr, consumer, rq);
    pthread_create(&c3, nullptr, consumer, rq);
    pthread_create(&c4, nullptr, consumer, rq);
    pthread_create(&p1, nullptr, productor, rq);
    pthread_create(&p2, nullptr, productor, rq);
    pthread_create(&p3, nullptr, productor, rq);
    pthread_create(&p4, nullptr, productor, rq);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(c4, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    pthread_join(p4, nullptr);
    return 0;
}